from distutils.core import setup as distutils_setup
from distutils.command.install import install
from distutils.command.build import build
from distutils.core import Extension

import numpy
import os

class CustomModuleBuildDirBuilder(build):
    def initialize_options(self):
        build.initialize_options(self)
        self.build_base = 'mod_build'


class CustomBuildDirInstaller(install):
    
    def finalize_options(self):
        install.finalize_options(self)
        
        src_cmd_obj = self.distribution.get_command_obj('build')
        src_cmd_obj.ensure_finalized()
        
        self.build_lib = src_cmd_obj.build_lib

lev_mar_srcs = "lm.c Axb.c misc.c lmlec.c lmbc.c lmblec.c lmbleic.c".split()

lev_mar_srcs = ['src/levmar/'+i for i in lev_mar_srcs]

numpyincludedirs = numpy.get_include()

list_of_references = [os.path.join('references',i) for i in os.listdir(os.path.join('src','doas','references'))]


distutils_setup(cmdclass={'install':CustomBuildDirInstaller, 'build':CustomModuleBuildDirBuilder},
      name='doas',
      version='0.0.1',
      description='Package for manipulating DOAS spectra.', 
      author = 'Nial Peters', 
      author_email='nonbiostudent@hotmail.com',
      package_dir = {'':'src'},
      packages=['doas','doas.plugins'],
      package_data={"doas":list_of_references},
#       ext_modules=[Extension("doas._shift_and_squeeze",
#                    ["src/swig/shift_and_squeeze_wrap.c","src/c_doas/shift_and_squeeze.c", "src/c_doas/autodealloc_numpy.c", "src/c_doas/traceback.c"],
#                    include_dirs=[numpyincludedirs]+["src/c_doas"],
#                    libraries=['blas','gsl','m', 'gomp'],
#                    extra_compile_args=['-fopenmp'],
#                    extra_link_args=['-fopenmp']),
#                    
#                    Extension("doas._fitting",
#                    ["src/swig/fitting_wrap.c","src/c_doas/fitting.c", "src/c_doas/autodealloc_numpy.c", "src/c_doas/traceback.c"],
#                    include_dirs=[numpyincludedirs]+["src/c_doas"],
#                    libraries=['blas','gsl','m', 'gomp'],
#                    extra_compile_args=['-fopenmp'],
#                    extra_link_args=['-fopenmp'])
#                    ]
      #ext_modules=[Extension("doas.shift_and_squeeze", 
      #                     ["src/c_doas/shift_and_squeeze.c", "src/c_doas/autodealloc_numpy.c"] +lev_mar_srcs,
       #                    include_dirs=[numpyincludedirs]+['src/levmar'],
      #                     libraries=['blas','gsl','m','lapack'])]
                
      )


try:
    from avoplot.plugins import setup as avoplot_setup
    
    class CustomPluginBuildDirBuilder(build):
        def initialize_options(self):
            build.initialize_options(self)
            self.build_base = 'plugin_build'
    
    avoplot_setup(cmdclass={'build':CustomPluginBuildDirBuilder},
                  name='AvoPlot DOAS Plugin',
                  version='0.0.1',
                  description='AvoPlot plugin for analysing DOAS spectra', 
                  author = 'Nial Peters', 
                  author_email='nonbiostudent@hotmail.com',
                  package_dir = {'':'src'},
                  packages=['avoplot_doas_plugin']
                  )
except ImportError:
    print "AvoPlot could not be found - skipping plugin installation"

